﻿using UnityEngine;
using System.Linq;

class ViewImplementation : IViewService
{
    public void Instantiate(string ObjectName)
    {
        Resources.Load<GameObject>(@"Prefabs\" + ObjectName);
    }
    public void InstantiateHitBox(IWeapon weapon, EntitasVec3 position, string parent, Direction direction)
    {
        string hitbox = null;
        GameObject hitboxview = null;
        switch (direction)
        {
            case Direction.Up:
                try
                {
                    position.y += 1f;
                    hitbox = weapon.Name + "_Hitbox_Up";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
                position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Up);
                }
                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");
                }
                break;
            case Direction.Up_Left:
                try
                {
                    position.y += 1f;
                   hitbox = weapon.Name + "_Hitbox_Up";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
                position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Up);
                }
                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");
                }
                break;
            case Direction.Up_Right:
                try
                {
                    position.y += 1f;
                   hitbox = weapon.Name + "_Hitbox_Up";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
                position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Up);
                }
                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");
                }
                break;
            case Direction.Down:

                try
                {
                    position.y -= 1f;
                    hitbox = weapon.Name + "_Hitbox_Down";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
               position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Down);
                }

                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");

                }
                break;
            case Direction.Down_Left:

                try
                {
                    position.y -= 1f;
                    hitbox = weapon.Name + "_Hitbox_Down";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
               position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Down);
                }

                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");

                }
                break;
            case Direction.Down_Right:

                try
                {
                    position.y -= 1f;
                    hitbox = weapon.Name + "_Hitbox_Down";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
               position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Down);
                }

                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");

                }
                break;
            case Direction.Left:
                try
                {
                    position.x -= 1f;
                    hitbox = weapon.Name + "_Hitbox_Left";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
               position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                   hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Left);
                }
                catch (System.Exception)
                {

                    Debug.Log("No Valid Weapon Attached");


                }

                break;
            case Direction.Right:
                try
                {
                    position.x += 1f;
                   hitbox = weapon.Name + "_Hitbox_Right";
                    hitboxview = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\WeaponPrefabs\" + hitbox),
               position, Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(parent);
                    hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(weapon.Damage);
                    if (hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile)
                        hitboxview.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(Direction.Right);
                }
                catch (System.Exception)
                {
                    Debug.Log("No Valid Weapon Attached");
                }

                break;
            default:
                break;
        }


    }

    public void InstantiateEnemyAttack(string EnemyName,int damage, EntitasVec3 position, Direction direction)
    {
        EnemyName = EnemyName.Remove(EnemyName.IndexOf(' '));
        string attackname = EnemyName + "_Attack";
        switch (direction)
        {
            case Direction.Left:
                try
                {
                    attackname += "_Left";
                    var hitbox = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\EnemyAttacks\" + attackname), position,
                   Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitbox.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(EnemyName);
                    hitbox.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(damage);


                    if (hitbox.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile == true)
                    {
                        hitbox.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(direction);
                    }
                }
                catch (System.Exception)
                {

                    Debug.Log("error in enemy attack");
                }
                break;
            case Direction.Right:
                try
                {
                    attackname += "_Right";
                    var hitbox = GameObject.Instantiate(Resources.Load<GameObject>(@"Prefabs\EnemyAttacks\" + attackname), position,
                   Quaternion.Euler(new EntitasVec3(0, 0, 0)), null);
                    hitbox.GetComponent<HitBoxBaseView>().Collisionentity.AddOwner(EnemyName);
                    hitbox.GetComponent<HitBoxBaseView>().Collisionentity.AddDamage(damage);

                    if (hitbox.GetComponent<HitBoxBaseView>().Collisionentity.isProjectile == true)
                    {
                        hitbox.GetComponent<HitBoxBaseView>().Collisionentity.AddDirection(direction);
                    }
                }
                catch (System.Exception)
                {

                    Debug.Log("error in enemy attack");
                }
                break;
            default:
                break;
        }


    }
}

